package com.co.video.service.impl;

import com.co.common.entity.Result;
import com.co.video.mapper.VideoMapper;
import com.co.video.pojo.Video;
import com.co.video.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * TvVideo业务层接口实现类
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;


    /**
     * TvVideo条件+分页查询
     *
     * @param video 查询条件
     * @param page  页码
     * @param size  页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Video> findPage(Video video, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(video);
        //执行搜索
        return new PageInfo<Video>(videoMapper.selectByExample(example));
    }

    /**
     * TvVideo分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Video> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Video>(videoMapper.selectAll());
    }

    /**
     * TvVideo条件查询
     *
     * @param video
     * @return
     */
    @Override
    public List<Video> findList(Video video) {
        //构建查询条件
        Example example = createExample(video);
        //根据构建的条件查询数据
        return videoMapper.selectByExample(example);
    }


    /**
     * TvVideo构建查询对象
     *
     * @param video
     * @return
     */
    public Example createExample(Video video) {
        Example example = new Example(Video.class);
        Example.Criteria criteria = example.createCriteria();
        if (video != null) {
            // 主键
            if (!StringUtils.isEmpty(video.getCid())) {
                criteria.andEqualTo("cid", video.getCid());
            }
            // vdetail的id
            if (!StringUtils.isEmpty(video.getAid())) {
                criteria.andEqualTo("aid", video.getAid());
            }
            // 第几个视频
            if (!StringUtils.isEmpty(video.getPage())) {
                criteria.andEqualTo("page", video.getPage());
            }
            // 视频来源
            if (!StringUtils.isEmpty(video.getFrom())) {
                criteria.andEqualTo("from", video.getFrom());
            }
            // 每个视频描述
            if (!StringUtils.isEmpty(video.getPart())) {
                criteria.andEqualTo("part", video.getPart());
            }
            // 视频时间（秒）
            if (!StringUtils.isEmpty(video.getDuration())) {
                criteria.andEqualTo("duration", video.getDuration());
            }
            // 视频源地址
            if (!StringUtils.isEmpty(video.getUrl())) {
                criteria.andEqualTo("url", video.getUrl());
            }
            // 创建时间
            if (!StringUtils.isEmpty(video.getCreateTime())) {
                criteria.andEqualTo("createTime", video.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(video.getUpdateTime())) {
                criteria.andEqualTo("updateTime", video.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        videoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改TvVideo
     *
     * @param video
     */
    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKey(video);
    }

    /**
     * 增加TvVideo
     *
     * @param video
     */
    @Override
    public void add(Video video) {
        videoMapper.insert(video);
    }

    /**
     * 根据ID查询TvVideo
     *
     * @param id
     * @return
     */
    @Override
    public Video findById(String id) {
        return videoMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询TvVideo全部数据
     *
     * @return
     */
    @Override
    public List<Video> findAll() {
        return videoMapper.selectAll();
    }

    @Override
    public List<Video> findByAid(String aid) {
        Video video = new Video();
        video.setAid(aid);
        return videoMapper.select(video);
    }

    @Override
    public List<Video> findCidByAid(String aid) {
        return videoMapper.findCidByAid(aid);
    }

}
